package cn.wolfcode.controller;


import cn.wolfcode.entity.Department;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yunan
 * @since 2023-02-13
 */
@RestController
@CrossOrigin //允许跨域请求
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @PostMapping("/departments")
    public JsonResult save(@RequestBody Department department){
        boolean ret = departmentService.save(department);
        return ret?JsonResult.success(null):JsonResult.error("新增失败");
    }

    @DeleteMapping("/departments/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        boolean ret = departmentService.removeById(id);
        return ret?JsonResult.success(null):JsonResult.error("删除失败");
    }

    @PutMapping("/departments/{id}")
    public JsonResult update(@PathVariable("id") Long id, @RequestBody Department department){
        department.setId(id);
        boolean ret = departmentService.updateById(department);
        return ret?JsonResult.success(null):JsonResult.error("删除失败");
    }

    @GetMapping("/departments")
    public JsonResult list(){
        List<Department> list = departmentService.list();
        return JsonResult.success(list);
    }

    @GetMapping("/departments/{id}")
    public JsonResult get(@PathVariable("id") Long id){
        Department department = departmentService.getById(id);
        return JsonResult.success(department);
    }


}

